package cn.team11.sdsPackager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.aliyun.alink.sdk.jsbridge.BoneCallback;
import com.aliyun.alink.sdk.jsbridge.methodexport.BaseBonePlugin;
import com.aliyun.alink.sdk.jsbridge.methodexport.MethodExported;
import com.imnjh.imagepicker.SImagePicker;
import com.imnjh.imagepicker.activity.PhotoPickerActivity;
import com.uuzuche.lib_zxing.activity.CaptureActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import cn.team11.util.ImageUtil;
import cn.team11.util.T11Log;
import cn.team11.util.imagePicker.SingleFileLimitInterceptor;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class T11ImagePickerPlugin extends BaseBonePlugin {


    static final String TAG = "BoneSystemPlugin";

    public static final String API_NAME = "T11ImagePickerPlugin";
    private BoneCallback imgSelCallback = null;
    private static final int REQUEST_CODE_QR_CODE = 0x1000;
    public static final int REQUEST_CODE_IMAGE = 101;
    private String baseFileName = "";
    private static final String ErrorCode = "110000";
    private String dstUrl;

    public void open(Activity activity) {
        Intent intent = new Intent(activity, CaptureActivity.class);
        activity.startActivityForResult(intent, REQUEST_CODE_QR_CODE);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        T11Log.d("requestCode = "+requestCode+", resultCode = "+resultCode+", data = "+data);
        switch (requestCode) {
            case REQUEST_CODE_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    final ArrayList<String> pathList =
                            data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT_SELECTION);
                    try {
                        ImageUtil.uploadImg(dstUrl,pathList.get(0), baseFileName, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                T11Log.d("上传照片失败");
                                imgSelCallback.failed(ErrorCode, "上传照片失败");
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                JSONObject obj = new JSONObject();
                                try {
                                    obj.put("ret", "success");
                                } catch (JSONException e) {}
                                T11Log.d("文件上传成功");
                                imgSelCallback.success(obj);
                            }
                        },jsBridge.getCurrentActivity());
                    } catch (IOException e) {
                        imgSelCallback.failed(ErrorCode, "上传照片失败");
                        T11Log.d("上传照片失败");
                    }
                } else {
                    imgSelCallback.failed(ErrorCode, "上传照片失败");
                    T11Log.d("上传照片失败");
                }
                break;
            default:
                break;
        }
    }


    @MethodExported
    public void selImg(String baseFileName, String dstUrl,BoneCallback boneCallback) {
        this.baseFileName = baseFileName;
        this.dstUrl =dstUrl;
        T11Log.d("baseFileName = "+baseFileName);
        imgSelCallback = boneCallback;
        try {
            SImagePicker
                    .from(jsBridge.getCurrentActivity())
                    .maxCount(1)
                    .rowCount(3)
                    .showCamera(true)
                    .pickMode(SImagePicker.MODE_IMAGE)
                    .fileInterceptor(new SingleFileLimitInterceptor())
                    .forResult(REQUEST_CODE_IMAGE);
        } catch (Exception e) {
            boneCallback.failed(ErrorCode, e.toString());
        }
    }
}
